/******************************************************************************
 * @file     gpio.c
 * @brief    Bootloader中gpio外设相关函数
 * @author   Ruilist
 * @date     2025-08-15
 * @version  V1.0.0
 * @copyright Copyright (c) 2025
 *
 * @note     1 tab = 4 space
 *
 * @par 日志:
 * - V1.0.0 2025-08-15 Ruilist
 *****************************************************************************/

//******************************** Includes *********************************//

#include "gpio.h"

//******************************** Includes *********************************//

//******************************** Declaring ********************************//

/**
 * @brief   key的io接口初始化，PA0，按下时为低电平
 *
 * @param   void
 *
 * @return  void
 *
 */
void Key_IO_Init( void )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Enable the GPIOA peripheral */
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE );
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init( GPIOA, &GPIO_InitStructure );
}

/**
 * @brief   led的io接口初始化，PC13
 *
 * @param   void
 *
 * @return  void
 *
 */
void Led_IO_Init( void )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Enable the GPIOC peripheral */
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE );
    GPIO_InitStructure.GPIO_Pin   = LED_C13_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init( LED_C13_PORT, &GPIO_InitStructure );
}

/**
 * @brief   所有gpio初始化
 *
 * @param   void
 *
 * @return  void
 *
 */
void gpio_init( void )
{
    Key_IO_Init();
    Led_IO_Init();
}

/**
 * @brief   扫描key的值
 *
 * @param   void
 *
 * @return  uint8_t
 * @retval  0 未按下
 * @retval  1 按下
 *
 */
uint8_t Key_Scan( void )
{
    if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_0 ) == Bit_RESET )
    {
        Delay( 20 );
        if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_0 ) == Bit_RESET )
        {
            return 1;
        }
    }
    return 0;
}

//******************************** Declaring ********************************//
